﻿// (c) Copyright Max Kazarin.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

/// <reference path='../Common/jquery-1.3.2.js' />
// <reference path='../Common/MicrosoftAjax.debug.js' />

Type.registerNamespace('jQueryNET.Controls');

jQueryNET.Controls.TextBox = function jQueryNET$Controls$TextBox(element, createSelf) {
	/// <summary>
	/// This class implement Textbox control
	/// </summary>
	/// <param name='element' type='Sys.UI.DomElement' domElement='true'>
	/// DOM element the control is associated with
	/// </param>

	jQueryNET.Controls.TextBox.initializeBase(this, [element]);

	this._rootElement
		.addClass('textbox')
		.addClass('form_field')
		.addClass('form_text');
	this._rootElement.attr({
		autocomplete: 'off'
	})
}

jQueryNET.Controls.TextBox.createElement = function(parent) {
	/// <summary>
	/// Static method for using on client-side. Create DOM element the control
	/// associated with
	/// </summary>
	/// <param name='parent' type='Sys.UI.DomElement' domElement='true'>
	/// Parent DOM element or jQuery selector
	/// </param>
	
	var element =
		$(parent)
			.append('<input type="text">')
			.find('input:last');

	return element.get(0);
}

function jQueryNET$Controls$TextBox$initialize() {
	/// <summary>
	/// Initialize the control
	/// </summary>
	
	jQueryNET.Controls.TextBox.callBaseMethod(this, 'initialize');
	this._rootElement
		.bind('focus', this, this._onFocus)
		.bind('blur', this, this._onBlur);
}

function jQueryNET$Controls$TextBox$dispose() {
	/// <summary>
	/// Dispose the control
	/// </summary>

	this._rootElement
		.unbind('focus', this, this._onFocus)
		.unbind('blur', this, this._onBlur);

	jQueryNET.Controls.TextBox.callBaseMethod(this, 'dispose');
}


function jQueryNET$Controls$TextBox$get_text() {
	/// <value type='String'>
	/// Button title
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._rootElement.attr('value');
}

function jQueryNET$Controls$TextBox$set_text(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', mayBeNull: false}]);
	if (e) throw e;

	this._rootElement.attr('value', value);
}

function jQueryNET$Controls$TextBox$get_maxLength() {
	/// <value type='String'>
	/// Max input value length
	/// </value>

	if (arguments.length !== 0) throw Error.parameterCount();
	return this._rootElement.attr('maxlength');
}

function jQueryNET$Controls$TextBox$set_maxLength(value) {
	var e = Function._validateParams(arguments, [{ name: 'value', type: Number, mayBeNull: false}]);
	if (e) throw e;

	if (value > 0)
		this._rootElement.attr('maxlength', value);
	else
		this._rootElement.removeAttr('maxlength');
}

function jQueryNET$Controls$TextBox$_onFocus() {
	$(this).addClass('form_focus');
}

function jQueryNET$Controls$TextBox$_onBlur() {
	$(this).removeClass('form_focus');
}

jQueryNET.Controls.TextBox.prototype = {
	initialize: jQueryNET$Controls$TextBox$initialize,
	dispose: jQueryNET$Controls$TextBox$dispose,

	get_text: jQueryNET$Controls$TextBox$get_text,
	set_text: jQueryNET$Controls$TextBox$set_text,
	get_maxLength: jQueryNET$Controls$TextBox$get_maxLength,
	set_maxLength: jQueryNET$Controls$TextBox$set_maxLength,

	_onFocus: jQueryNET$Controls$TextBox$_onFocus,
	_onBlur: jQueryNET$Controls$TextBox$_onBlur
}

jQueryNET.Controls.TextBox.registerClass('jQueryNET.Controls.TextBox', jQueryNET.Controls.ControlBase);